import { defineStore } from 'pinia'
import { ref } from 'vue'
import service from '../utils/request'
import { ElMessage } from 'element-plus'

export const useOrderStore = defineStore('/customer/orders', () => {
    const orderList = ref([])
    const getOrderList = async (userId) => {
        if (userId) {
            const res = await service.get('/customer/bookings', { params: { userId } }) 
            if (res.data.code === 200) {
                orderList.value = res.data.data
                return orderList.value 
            }else{
                ElMessage.error(res.data.message)
            }
        }
    }

    const createOrder = async (submit) => {
        const res = await service.post('/customer/bookings', submit )
        if (res.data.code === 200) {
            ElMessage.success('创建成功')
            return res.data.data 
        }else{
            ElMessage.error(res.data.message)
        }
    }

    const payOrder = async (id) => {
        const params = { id }
        const res = await service.post('/customer/bookings/pay', params )
        if (res.data.code === 200) {
            ElMessage.success('支付成功')
            return res.data.data
        }else{
            ElMessage.error(res.data.message)
        }
    }

    const cancelOrder = async (id) => {
        const params = { id }
        const res = await service.post('/customer/bookings/cancel', params )
        if (res.data.code === 200) {
            ElMessage.success('取消成功')
            return res.data.data
        }else{
            ElMessage.error(res.data.message)
        }
    }

    const refundOrder = async (id) => {
        const params = { id }
        const res = await service.post('/customer/bookings/refund', params )
        if (res.data.code === 200) {
            ElMessage.success('退款成功')
            return res.data.data 
        }else{
            ElMessage.error(res.data.message)
        }
    }

    const confirmRefund = async (id) => {
        const params = { id }
        const res = await service.post('/customer/bookings/confirmRefund', params ) 
        if (res.data.code === 200) {
            ElMessage.success('退款成功')
            return res.data.data 
        }else{
            ElMessage.error(res.data.message)
        }
    }

    const getStatues = async (roomId, date) => {
        const res = await service.get('/customer/rooms/status', { params: { roomId: roomId, date: date } })
        if (res.data.code === 200) {
            return res.data.data
        }else{
            ElMessage.error(res.data.message)
        }
    }
    return { orderList, getOrderList, payOrder, cancelOrder, refundOrder, confirmRefund, createOrder, getStatues }
})